package com.haierCamera.customapplication.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.api.vo.GetPushEntity;
import com.haierCamera.customapplication.databinding.HzklActivityUserMsgSettingBinding;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLUserMsgSettingActivity extends BaseActivity {
    private HzklActivityUserMsgSettingBinding binding;
    private List<String> item1 = new ArrayList<>();
    private List<String> item2 = new ArrayList<>();
    private List<String> item3 = new ArrayList<>();
    DecimalFormat df = new DecimalFormat("00");
    @Inject
    UserRepo repo;
    private String start = "00:00";
    private String end = "00:00";
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_user_msg_setting);
        initData();
    }

    int stat = 0;

    private void initData() {
        initTime();
        binding.registerBack.setOnClickListener(v -> finish());
        binding.msgTimeLayout.setOnClickListener(view -> showTime());
        binding.switchMsg.setOnCheckedChangeListener((compoundButton, b) -> {
            if (!compoundButton.isPressed()) {
                return;
            }
            if (compoundButton.isChecked()) {
                stat = 1;
            } else {
                stat = 0;
            }
            setPush();
        });
        getSetting();
    }

    private void setPush() {
        repo.setSendSetting(stat, start, end).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    // Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
                    setTime();
                    break;
            }
        });
    }

    private void setTime() {
        if (TextUtils.isEmpty(start) || TextUtils.isEmpty(end)) {
            return;
        }
        String all = start.replaceAll(" ", "").replaceAll(":", "");
        int parseStart = Integer.parseInt(start.replaceAll(" ", "").replaceAll(":", ""));
        int parseEnd = Integer.parseInt(end.replaceAll(" ", "").replaceAll(":", ""));
        if (parseEnd < parseStart) {
            binding.msgTime.setText(String.format("%s-次日%s", start.replaceAll(" ", ""), end.replaceAll(" ", "")));
        } else {
            binding.msgTime.setText(String.format("%s-%s", start.replaceAll(" ", ""), end.replaceAll(" ", "")));
        }
    }

    private void getSetting() {
        repo.getSendSetting().observe(this, getPushEntityResource -> {
            switch (getPushEntityResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, getPushEntityResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(getPushEntityResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    GetPushEntity data = getPushEntityResource.data;
                    // 00关闭 1开启
                    assert data != null;
                    if (data.pushState == 0) {
                        binding.switchMsg.setChecked(false);
                        stat = 0;
                    } else {
                        stat = 1;
                        binding.switchMsg.setChecked(true);
                    }
                    start = data.startTime;
                    end = data.endTime;
                    setTime();
                    break;
            }
        });
    }

    int time1 = 0, time2 = 0;

    private void initTime() {
        item2.add("~");
        for (int i = 0; i < 24; i++) {
            time1 = i;
            String format1 = df.format(time1);
            for (int f = 0; f < 60; f += 10) {
                time2 = f;
                String format2 = df.format(time2);
                item1.add(format1 + "  :  " + format2);
                item3.add(format1 + "  :  " + format2);
            }
            time2 = 0;
        }
    }

    private void showTime() {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(HZKLUserMsgSettingActivity.this, (options1, option2, options3, v) -> {
            start = item1.get(options1);
            end = item1.get(options3);
            //返回的分别是三个级别的选中位置
            int parseStart = Integer.parseInt(start.replaceAll(" ", "").replaceAll(":", ""));
            int parseEnd = Integer.parseInt(end.replaceAll(" ", "").replaceAll(":", ""));
            if (parseEnd < parseStart) {
                binding.msgTime.setText(String.format("%s-次日%s",  start.replaceAll(" ", ""), end.replaceAll(" ", "")));
            } else {
                binding.msgTime.setText(String.format("%s-%s",  start.replaceAll(" ", ""), end.replaceAll(" ", "")));
            }
            setPush();
        }).build();
        pvOptions.setNPicker(item1, item2, item1);
        pvOptions.show();
    }
}
